// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   SNTask.java

package com.aide.task;

import com.aide.security.AideSNManager;
import com.aide.security.auth.AideRead;
import com.aide.security.auth.SNService;
import org.apache.log4j.Logger;
import org.phprpc.PHPRPC_Client;

public class SNTask {

	private static Logger log = Logger.getLogger(SNTask.class);
	private String snApplyAddr;
	public static String SN_APPLY_ADDRESS = "";

	public SNTask() {
		snApplyAddr = "";
	}

	public int apply() {
		String key;
		String mac;
		if (snApplyAddr == null || snApplyAddr.trim().equals(""))
			snApplyAddr = SN_APPLY_ADDRESS;
		AideSNManager.load();
		if (AideSNManager.isAuthorized()) {
			return 1;
		}
		key = AideSNManager.getUser();
		mac = AideSNManager.mac();
		if (key == null || key.trim().equals("")) {
			key = "test";
			mac = AideRead.mac(key);
		}
		PHPRPC_Client client = new PHPRPC_Client(snApplyAddr);
		client.setKeyLength(128);
		client.setEncryptMode(1);
		SNService snService = (SNService) client.useService(SNService.class);
		String jsonsn = snService.apply(key, mac, "");
		String sns[] = jsonsn.split("\\|");
		if (jsonsn == null || sns.length <= 1) {
			log.info("通过网络授权失败！无序列号");
			return -2;
		}
		key = sns[0];
		mac = sns[1];
		String sn = null;
		if (sns.length == 3)
			sn = sns[2];
		if (sn == null || sn.trim().equals("")) {
			log.info("通过网络授权失败！无序列号");
			return -2;
		}
		if (!AideRead.v(key, sn)) {
			try {
				log.info("通过网络授权失败！序列号错误或过期");
				return -4;
			} catch (Exception e) {
				log.info("通过网络授权，连接错误！");
				return -1;
			}
		}
		AideSNManager.setUser(key);
		AideSNManager.setSn(sn);
		log.info("通过网络授权成功！");
		return 1;

	}

	public String getSnApplyAddr() {
		return snApplyAddr;
	}

	public void setSnApplyAddr(String snApplyAddr) {
		this.snApplyAddr = snApplyAddr;
	}

}